package ngat.oss.simulation;

import java.util.*;

/** Implementation of TimeSignalGenerator which delegates the request to another TSG.*/
public class SlaveTimeSignalGenerator implements TimeSignalGenerator {

    /** The master TimeSigGenerator to which this TSG is a slave.*/
    TimeSignalGenerator master;

    TimeSignalListener tsl;

    /** Create a FastTimeSignalGenerator.*/
    public SlaveTimeSignalGenerator(TimeSignalGenerator master) {
	this.master = master;
    }

    /** Returns the time from the master.*/
    public long getTime() { return master.getTime(); }

    /** Implementors should register the listener for a signal at the speicified time.*/
    public void awaitTimingSignal(TimeSignalListener tsl, long time) {
	this.tsl = tsl;
	System.err.println("STSG.awaitTimingSignal("+ScheduleSimulator.sdf.format(new Date(time))+") , relay to master...");
	master.awaitTimingSignal(tsl, time);
    }

    /** Update the time, notify any awaiting listeners.*/
    public void updateTime(long updatedTime) {

	System.err.println("STSG.update("+ScheduleSimulator.sdf.format(new Date(updatedTime))+")");

	if (tsl == null)
	    return;
	System.err.println("STSG.update()::Notification to: "+tsl);
	// Signal the listener with its requested time.
	tsl.timingSignal(updatedTime);
    }

}

